package com.example.springboot;

import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

import java.net.InetAddress;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    /**
     * 启动成功后打印Swagger访问地址
     */
    @Bean
    public ApplicationRunner applicationRunner(Environment environment) {
        //Swagger接口文档访问地址
        //http://localhost:8080/swagger-ui/index.html
        //http://localhost:8080/doc.html
        return args -> {
            String host = InetAddress.getLocalHost().getHostAddress();
            String port = environment.getProperty("server.port", "8080");
            String contextPath = environment.getProperty("server.servlet.context-path", "");

            System.out.println("\n----------------------------------------------------------");
            System.out.println("  应用启动成功!");
            System.out.println("  本地访问地址: \thttp://localhost:" + port + contextPath);
            System.out.println("  外部访问地址: \thttp://" + host + ":" + port + contextPath);
            System.out.println("  Swagger文档: \thttp://localhost:" + port + contextPath + "/swagger-ui/index.html");
            System.out.println("  Knife4j文档: \thttp://localhost:" + port + contextPath + "/doc.html");
            System.out.println("----------------------------------------------------------\n");
        };
    }
}
